// 引入接口文件
import {
    getActivityAreaCount,
    getActivityAreaCountTrends,
    getActivityAreaComeInCount,
    getActivityAreaColumnCount,
    activityAreaexport
} from './activityAreaStatisticsServ.js'
// 日期格式化
import { DateUtil } from '../../../../utils/dateUtil'
const formatDate = DateUtil.formatDate
// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import config from 'config/config';
import { download } from 'utils/util'
import { makeAddress } from "utils/util"
import { detailServ } from "pages/marketingAdmin/liveAdmin/liveDetail/liveDetailServ";

// 初始默认状态
const defultState = {
    barDataList: [], // 柱形图数据
    detail: {
        visitPersonCount: 0,
        visitCount: 0,
        sharePersonCount: 0,
        shareCount: 0,
    },
    exportLoading: false,
    loading: false,
    areaList: [],
    checkBtn: true,
    btnLoading: false,
    actionType: '',
    biz: {
        queryForm: {}
    },
    modalObj: {
        visible: false,
    },
    codeList: {}, //验证码返回数据
    codeQuery: {}, //验证码
    provinceData: [],
    markers: [],
    province: null,
    center: {},
    listData: [], //表格数据
    activityAreaId: '',
    startTime: '',
    endTime: '',
    linexAxis: [], //趋势横坐标
    customTime: '',
    ComeInCount: 1,
    CountTrends: 1,
}

let formatSeconds = function (value) {

    let seconds = parseInt(value);
    let minutes = 0;
    let hours = 0;
    let result = "";

    if (seconds > 60) {
        minutes = parseInt(seconds / 60);
        seconds = parseInt(seconds % 60);
        if (minutes > 60) {
            hours = parseInt(minutes / 60);
            minutes = parseInt(minutes % 60);
        }
    }

    if (seconds > 0) {
        result = parseInt(seconds) + "秒";
    }
    if (minutes > 0) {
        result = "" + parseInt(minutes) + "分" + result;
    }
    if (hours > 0) {
        result = "" + parseInt(hours) + "小时" + result;
    }

    return result;
};

// 声明module
const tmpModule = {
    // 默认状态
    state: cloneDeep(defultState),
    // 入口函数(先执行)
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname, query }) => {
                let { actionType, id } = query
                // 页面路由匹配时执行
                if ('' + tmpModule.pathname === '' + pathname) {
                    let nowDate = new Date().getTime()
                    let startTime = formatDate(nowDate - 3600 * 24 * 7 * 1000)
                    let endTime = formatDate(nowDate)
                    dispatch({ type: 'updateStore', payload: cloneDeep(defultState) })
                    dispatch({
                        type: 'updateStore',
                        payload: {
                            activityAreaId: id,
                            startTime,
                            endTime,
                        }
                    });
                    dispatch({
                        type: 'getActivityAreaCount', payload: {
                            activityAreaId: id,
                            startTime,
                            endTime
                        }
                    });
                    dispatch({
                        type: 'getActivityAreaCountTrends',
                        payload: {
                            activityAreaId: id,
                            startTime,
                            endTime,
                            type:1
                        }
                    });
                    dispatch({
                        type: 'getActivityAreaComeInCount', payload: {
                            activityAreaId: id,
                            startTime,
                            endTime,
                            type:1
                        }
                    });
                    dispatch({
                        type: 'getActivityAreaColumnCount', payload: {
                            activityAreaId: id,
                            startTime,
                            endTime
                        }
                    });
                } // end if
            }) // end listen
        },
    },

    // 方法列表
    effects: {
        // 概况
        * getActivityAreaCount({ payload }, { put, call, select }) {
            try {
                let { success, data } = yield call(getActivityAreaCount, payload)
                if (success) {
                    yield put({
                        type: 'updateStore',
                        payload: { detail: data }
                    })
                }

            } catch (e) {
                console.log(e)
            }
        },
        // 折线图
        * getActivityAreaCountTrends({ payload }, { put, call, select }) {
            try {
                let xAXI = [], data1 = [], data2 = [], data3 = [], data4 = [], total = [], lineData = []
                let name = ['分享次数', '分享人数', '访问次数', '访问人数']
                let { success, rows } = yield call(getActivityAreaCountTrends, payload)
                if (success) {
                    for (let i = 0; i < rows.length; i++) {
                        xAXI.push(rows[i].createTime)
                        data1.push(rows[i].shareCount)
                        data2.push(rows[i].sharePersonCount)
                        data3.push(rows[i].visitCount)
                        data4.push(rows[i].visitPersonCount)
                    }
                    total.push(data1, data2, data3, data4)
                    for (let i = 0; i < name.length; i++) {
                        let opt = {
                            name: name[i],
                            type: 'line',
                            stack: '',
                            // areaStyle: {},
                            data: total[i]
                        }
                        lineData.push(opt)
                    }
                    yield put({
                        type: 'updateStore',
                        payload: { lineDataList: lineData, linexAxis: xAXI }
                    })
                }

            } catch (e) {
                console.log(e)
            }
        },
        // 柱形图
        * getActivityAreaComeInCount({ payload }, { put, call, select }) {
            try {
                let { success, data } = yield call(getActivityAreaComeInCount, payload)
                if (success) {
                    if (JSON.stringify(data) == '{}' || !(data instanceof Object)) { return false }
                    let barDataList = []
                    barDataList.push(data.scanCount) //扫码
                    barDataList.push(data.shareCount) //分享--会话
                    barDataList.push(data.initiativeCount) //主动点击
                    barDataList.push(data.otherCount)//其他
                    yield put({
                        type: 'updateStore',
                        payload: { barDataList: barDataList }
                    })
                }

            } catch (e) {
                console.log(e)
            }
        },
        // 表格
        * getActivityAreaColumnCount({ payload }, { put, call, select }) {
            try {
                let { success, rows } = yield call(getActivityAreaColumnCount, payload)
                if (success) {
                    yield put({
                        type: 'updateStore',
                        payload: { listData: rows }
                    })
                }

            } catch (e) {
                console.log(e)
            }
        },
        // 导出
        *activityAreaexport({ payload }, { put, call, select }) {
            try {
                let { activityAreaId, startTime, endTime } = yield select(state => state[tmpModule.namespace])
                let { success, data } = yield call(activityAreaexport, activityAreaId, startTime, endTime)
                if (success) {
                    // window.open(data)
                    window.location.href = data
                }
            } catch (e) {
                console.log(e)
            }
        },
    },

    // 存入状态机
    reducers: {
        updateStore(preState, action) {
            return Object.assign({}, preState, action.payload)
        },
    }
}

export default tmpModule